package com.hdu.dwh.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.hdu.dwh.pojos.LoginSupersetUser;
import com.hdu.dwh.pojos.SupersetUser;
import com.hdu.dwh.results.ResponseResult;
import com.hdu.dwh.service.SupersetRestService;

import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.ws.rs.FormParam;
import lombok.RequiredArgsConstructor;

@RestController
@RequestMapping("/subject/table")
@RequiredArgsConstructor
@Tag(name = "Superset")
@Deprecated
public class SupersetController {

    @Autowired
    private SupersetRestService supersetRestService;

    @PostMapping("/register")
    public ResponseResult<?> registerUser(@RequestBody SupersetUser entity) {
        MultiValueMap<String, String> form = new LinkedMultiValueMap<>();
        form.add("first_name", entity.getFirst_name());
        form.add("last_name", entity.getLast_name());
        form.add("username", entity.getUsername());
        form.add("active", String.valueOf(entity.getActive()));
        form.add("email", entity.getEmail());
        form.add("roles", String.valueOf(entity.getRoles()));
        form.add("password", entity.getPassword());
        form.add("conf_password", entity.getConf_password());

        ResponseEntity<String> supersetUser = this.supersetRestService.createSupersetUser(form);
        if (supersetUser.getStatusCode().is2xxSuccessful() || supersetUser.getStatusCode().is3xxRedirection()) {
            return ResponseResult.success("注册成功");
        }
        return ResponseResult.fail("注册失败, 状态码: " + supersetUser.getStatusCode());

    }

    @PostMapping("/login")
    public ResponseResult<?> login(@RequestBody LoginSupersetUser entity) {
        MultiValueMap<String, String> form = new LinkedMultiValueMap<>();
        form.add("username", entity.getUsername());
        form.add("password", entity.getPassword());
        ResponseEntity<String> response = this.supersetRestService.loginSuperset(form);
        if (response.getStatusCode().is2xxSuccessful() || response.getStatusCode().is3xxRedirection()) {
            List<String> cookies = response.getHeaders().get(HttpHeaders.SET_COOKIE);
            return ResponseResult.success(cookies);
        }
        return ResponseResult.fail("登录失败, 状态码: " + response.getStatusCode());
    }

}
